package com.yk.filedownload.util;


import org.apache.tika.mime.MimeType;
import org.apache.tika.mime.MimeTypes;

/**
 * Project Name:cykfiledownload
 * File Name:FileType
 * Package Name:com.yk.filedownload.util
 * Date:2017\12\8 0008
 * Author:dengtianjia
 * Description:
 * Copyright (c) 2017, 重庆云凯科技有限公司 All Rights Reserved.
 */
public class FileType {

    /**
     * 获取下载文件的后缀名
     * @param contentType
     * @return
     * @throws Exception
     */
    public static String getExt(String contentType , String url) throws Exception {
        String[] arrs = url.split("\\.");

        if(arrs[arrs.length-1].length() <= 6){
            return arrs[arrs.length-1];
        }else if(contentType.equals("application/octet-stream")){
            return getFileNames(url)[1];
        }else {
            MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();

            MimeType type = allTypes.forName(contentType);

            return type.getExtension();
        }
    }


    public static String[] getFileNames(String url){
        String[] strs = url.split("/");
        String last = strs[strs.length - 1];
        String[] last_strs =  last.split("\\.");
        return last_strs;
    }


}
